/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import helpers.Response;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.User;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;


/**
 *
 * @author luci
 */

@Named(value = "adminUsersBean")
@SessionScoped
public class AdminUsers implements Serializable {
    private static Logger logger = LogManager.getLogger(AdminUsers.class.getName());
    
    private User user;
    private Integer initialIndex = 0;
    private List<User> users = new ArrayList<User>();
    private int numberOfUsers = 10;
    private String search;
    private boolean searchError;

    public boolean isSearchError() {
        return searchError;
    }

    public void setSearchError(boolean searchError) {
        this.searchError = searchError;
    }
     
    
    public Integer getInitialIndex() {
        return initialIndex;
    }

    public void setInitialIndex(Integer initialIndex) {
        this.initialIndex = initialIndex;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }    
    
    /**
     * Creates a new instance of AdminUsersBean
     */
    public AdminUsers() {
    }
    
    public void searchUsers(){
       searchError = false;
       initialIndex = 0;            
       List<User> newUsers = User.getUsersByName(search, initialIndex, numberOfUsers);          
       if (newUsers == null ){
           searchError = true;
           users = new ArrayList<User>();
       }else {
           users=newUsers;
       }       
    }
    
    public void next(){
        if (search == null || search.equals(""))
            return;
        searchError = false;
        if(users.size() == numberOfUsers){
            initialIndex+=numberOfUsers;
        }else {
            return;
        }
                           
        List<User> newUsers = User.getUsersByName(search, initialIndex, numberOfUsers);       
        if (newUsers == null){
            searchError = true;
        }else{
            users=newUsers;
        }     
        
    }
    
    public void previous(){
        if (search == null || search.equals("")) 
            return;
        if (initialIndex <= numberOfUsers) 
            initialIndex = 0;
        else 
            initialIndex-=numberOfUsers;
        searchError = false;
        initialIndex = 0;            
        List<User> newUsers = User.getUsersByName(search, initialIndex, numberOfUsers);       
        if(newUsers == null){
            searchError = true;
        }else{
            users = newUsers;
        }           
    }
    
    public String deleteUser(User u){        
        int res = u.delete();        
        searchError = false;        
        List<User> newUsers =  User.getUsersByName(search, initialIndex, numberOfUsers);   
        if(users==null){
           searchError=true; 
        }else{
           users = newUsers;           
        }
        
        if(res >= 1){
            logger.info("Deleted User with ID "+u.getId()+" and email "+u.getEmail());
            return Response.SUCCESS;
        }else{
            logger.info("Failed deleting User with ID "+u.getId()+" and email "+u.getEmail());
            return Response.FAIL;
        }       
    }
    public String createUser() 
    {
        int id = user.insert();
        if(id >= 1){
            logger.info("Created New User with ID "+user.getId()+" and email "+user.getEmail());
            return Response.SUCCESS;
        }else     
            return Response.FAIL;
    }
    
    public String viewUser(User u){
        return null;
    }
    
}
